$(function () {
    //首页主题和推文
    function loadIndex() {
        $.ajax({
            type: "get",
            url: domain.test + "/getindex",
            dataType: 'json',
            // url:domain.online + "/api/index",
            success: function (response) {
                if (response.data.rtnCode === 200) {
                    // if(response.rtnCode===200) {
                    var indexDetail = response.data.data;
                    // var indexDetail = response.data;
                    localStorage.setItem("search",indexDetail.search);
                    $(".searchInput").html(indexDetail.search);
                    $(".customZone").css({ "background-color": "#fff" });
                    $(".customZone").html(template("customZone", { customZonePic: indexDetail.customPic }));
                    $(".themeBox").html(template("themeBox", { theme: indexDetail.themes }));
                    $(".contenBox").html(template("contenBox", { contentList: indexDetail.contents }));
                    pull.pullDownSuccess();       //刷新成功
                }
            }
        });
    }
    loadIndex();
    var pull = new Pull($(".contentBox"), {
        onPullDown: function () {
            loadIndex();
        }
    });
    //猜你喜欢
    var flag = false;
    var pageNum = 1;
    function loadData() {
        if (flag) return false;
        $.ajax({
            url: domain.test + "/getlike",
            // url: domain.online + "/api/index/like",
            type: 'get',
            data: 'pageNum=' + pageNum,
            dataType: 'json',
            beforeSend: function () {
                flag = true;
            },
            success: function (response) {
                var likeProductDetail = response.data.data;
                // var likeProductDetail = response.data;
                if (pageNum == 1) {
                    $(".likeIcon").css({ "display": "block" });
                    $('.likeProduct').html(template('likeProduct', { productList: likeProductDetail.plist }));
                } else {
                    $('.likeProduct').append(template('likeProduct', { productList: likeProductDetail.plist }));
                }
                pullmaster.pullDownSuccess();       //刷新成功
                if (likeProductDetail.plist.length < 10 || likeProductDetail.plist.length === 0) {
                    pullmaster.pullUpDone();        //到底啦
                    $(".bb_pull-up").css({"display":"none"});
                    $(".end").css({"display":"block"});
                }
                ++pageNum;
                flag = false;
            },
            error: function () {
                pullmaster.pullDownFailed();        //刷新失败
                pullmaster.pullUpFailed();          //数据加载失败
            }
        })
    }
    // 下拉刷新上滑分页加载
    var pullmaster = new Pull($('.likeProduct'), {
        onPullUp: function () {
            loadData();
        }
    });
    //跳转分类页面
    $(".classifyBtn").on("click",function() {
        location.href = "./view/classify/classify.html";
    });
    //主题详情
    $(".themeBox").on("click", ".themePic", function () {
        var themeId = $(this).attr("data-id");
        location.href = "./view/point/theme.html?id=" + themeId;
    });
    //主题商品详情
    $(".themeBox").on("click", ".themeProItem", function () {
        var pId = $(this).attr("data-id");
        location.href = "./view/classify/product.html?id=" + pId;
    });
    //推文详情
    $(".contenBox").on("click", ".contentItem", function () {
        var contentId = $(this).attr("data-id");
        location.href = "./view/point/content.html?id=" + contentId;
    });
    //猜你喜欢商品详情
    $(".likeProduct").on("click", ".likeProductItem", function () {
        var pId = $(this).attr("data-id");
        location.href = "./view/classify/product.html?id=" + pId;
    });
    //搜索页面跳转
    $(".searchInput").on("click",function(){
        location.href = "./view/search/search.html";
    });
    //消息中心页面跳转
    $(".msgIcon").on("click",function() {
        location.href = "./view/me/msgCenter.html";
    });
    //登录超时遮罩层
    $(".outTimeMarsk").css({
        "width":window.screen.width,
        "height":window.screen.height,
    });
    //登录超时显示遮罩层
    var outTimeCode = tools.queryString().outTimeCode;
    if(parseInt(outTimeCode)===401||parseInt(outTimeCode)===400) {
        $(".outTimeMarsk").css("display","block");
    }
    //继续逛逛
    $(".leftBtn").on("click",function() {
       location.href = "./index.html";
       localStorage.token = "";
    });
    //重新登录
    $(".rightBtn").on("click",function(){
        location.href = "./view/loginAndReg/login.html";
    });
})